#pragma once

#include <string.h>
#include <cassert>
#include <iostream>

namespace han
{
	class string
	{
	private:
		char* _str;
		size_t _capacity;
		size_t _size;
		static const  size_t npos;

	public:
		string() :_str(nullptr), _capacity(1), _size(0)
		{
			_str = new char[1] {'\0'};
		}

		string(const char* str = "") :_size(strlen(str))
		{
			_capacity = _size == 0 ? 1 : _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		string(const string& str, size_t pos, size_t len = npos)
		{
			if (pos >= str._size)
			{
				_str = new char[1] {'\0'};
				_capacity = 1;
				_size = 0;
				return;
			}
			len = (len == npos || len > str._size - pos) ? str._size - pos : len;
			_size = _capacity = len;
			_str = new char[len + 1];
			memcpy(_str, str._str + pos, len);
			_str[len] = '\0';
		}

		string(const char* str, size_t n)
		{
			_size = _capacity = n;
			_str = new char[n + 1];
			memcpy(_str, str, n);
			_str[n] = '\0';
		}

		string(size_t n, char c)
		{
			_size = n;
			_capacity = n;
			_str = new char[n + 1];
			for (size_t i = 0; i < n; ++i) {
				_str[i] = c;
			}
			_str[n] = '\0';
		}

		string(const string& s) :_size(s._size), _capacity(s._capacity)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
		}

		const char operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		char operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator>=(const string& s) const
		{
			return *this > s && *this == s;
		}

		bool operator<=(const string& s) const
		{
			return !(*this >= s);
		}
		bool operator<(const string& s) const
		{
			return !(*this >= s);
		}
		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		void reserve(size_t n)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
		}

		void resize(size_t n, char ch = '\0')
		{
			if (n == _size)
			{
				return;
			}
			else if (n < _size)
			{
				_size = n;
				_str[_size] = '\0';
			}
			else if (n > _size)
			{
				if (n > _capacity)
				{
					reserve(n);
				}
				size_t i = _size;
				while (i < n)
				{
					_str[i] = ch;
					++i;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}

		void push_back(char ch)
		{
			if (_size + 1 > _capacity)
			{
				reserve( _capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}

		void append(char ch) {
			if (_size + 1 > _capacity) 
			{
				reserve( _capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		const char* c_str()
		{
			return _str;
		}

		string& insert(size_t pos, char ch)
		{
			if (pos > _size)
				return *this;
			if (_size + 1 > _capacity)
			{
				reserve(_capacity * 2);
			}
			size_t end = _size + 1;
			while (end>pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = ch;
			++_size;
			
			return *this;
		}

		string& insert(size_t pos, const char* str)
		{
			if (pos > _size)
				return *this;
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size + len;
			while (end > pos + len-1)
			{
				_str[end] = _str[end - len];
				--end;
			}
			strncpy(_str + pos, str, len);
			_size += len;
			return *this;
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		friend std::ostream& operator<<(std::ostream& out, const string& s);
		friend std::istream& operator>>(std::istream& in, string& s);
	};
	const size_t string::npos = -1;

	std::ostream& operator<<(std::ostream& out, const string& s)
	{
		for (auto ch : s)
		{
			out << ch;
		}
		return out;
	}

	
	std::istream& operator>>(std::istream& in, string& s)
	{
		s.clear();

		char ch = in.get();
		char buff[128] = {0};
		size_t i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[127] = '\0';
				s += buff;
				i = 0;
			}

			ch = in.get();
		}

		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}
	
}